package com.dc.aimc.auth.security;

import com.dc.aimc.auth.entity.OauthClientDetails;
import com.dc.aimc.auth.service.OauthClientDetailsService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.NoSuchClientException;
import org.springframework.stereotype.Service;


/**
 * 自定义获取客户端详细信息服务
 */
@Log4j2
@Service
public class AimcClientDetailsService implements ClientDetailsService {

    @Autowired
    private OauthClientDetailsService oauthClientDetailsService;

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        // 查询客户端信息
        OauthClientDetails clientDetails = oauthClientDetailsService.getById(clientId);
        if (clientDetails == null) {
            throw new NoSuchClientException("未查询到客户端信息");
        }
        log.info("获取客户端详细信息: {}", clientDetails);
        return clientDetails;
    }
}
